import java.sql.*;

public class DatabaseConnection {
    /**
     * 数据库连接类，完成mysql数据库的连接并提供唯一的jdbc接口
     *
     * @author zj
     */
    private Connection con;
    private static DatabaseConnection instance;

    public Connection getCon() {
        return con;
    }

    public void setCon(Connection con) {
        instance.con = con;
    }

    public static DatabaseConnection getInstance() {
        if (instance == null) instance = new DatabaseConnection();
        return instance;
    }

    private DatabaseConnection()
    {
        String driverName="com.mysql.cj.jdbc.Driver";
        String dbURL="jdbc:mysql://39.104.170.158:3306/library?characterEncoding=utf-8&useSSL=false" +
                "&serverTimezone=UTC";
//        String driverName="com.mysql.cj.jdbc.Driver";
//        String dbURL="jdbc:mysql://localhost:3306/Library?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
        String userName="root";
        String userPwd="123456";
        try
        {
            Class.forName(driverName);
            System.out.println("加载驱动成功！");
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("加载驱动失败！");
        }
        try{
            this.con=DriverManager.getConnection(dbURL,userName,userPwd);
            System.out.println("连接数据库成功！");
        }catch(Exception e)
        {
            e.printStackTrace();
            System.out.print("MySQL连接失败！");
        }
    }


}
